package com.yj.springbootstudy.algorithm.array;

import java.util.Arrays;

/**
 * 使用数组模拟队列
 * 队列：先进先出，有序，可重复
 */
public class Demo02 {

    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.pushData(1);
        arrayList.pushData(2);
        arrayList.pushData(3);
        arrayList.pushData(4);
        arrayList.show();

        new java.util.ArrayList<>();
    }


}

class ArrayList{

    private int [] arr;
    private int front; //队列头
    private int rear;  //队列尾

    public ArrayList(){
        this.arr = new int[5];
    }

    //队列为空的条件：队列头 == 队列尾
    //队列为满的条件：(队列尾+1) % arr.length == 队列头

    public void pushData(int data){
        if ((rear + 1) % arr.length == front){
            throw new RuntimeException("当前队列已满");
        }
        arr[rear++] = data;
        rear = rear % arr.length;
    }

    public int getFirstData(){
        if (rear == front){
            throw new RuntimeException("当前队列为空");
        }
        int result = arr[front++];
        front = front % arr.length;
        return result;
    }

    //todo 删除
//    public void deleteData(int data){
//        if (rear == front){
//            throw new RuntimeException("当前队列为空");
//        }
//        for (int i = 0; i < (rear + 1) % arr.length ; i++) {
//            if (arr[i] == data){
//
//            }
//        }
//    }

    public void show(){
        System.out.println(Arrays.toString(arr));
    }

    public boolean expansion(){
        if ((rear + 1) % arr.length == front){
            int newArr[] = new int[arr.length * 2];
            System.arraycopy(arr,0,newArr,0,arr.length);
            return true;
        }
        return false;
    }
}
